package com.itcast.donkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;

/**
 * Created by lanouhn on 16/9/6.
 */
public class ToolRateActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private ImageView ivRateImage;
    private TextView tvZimu;
    private TextView tvRateMoney;

    private EditText china;
    private EditText usd;
    private EditText eur;
    private EditText random;

    private int id;

    private double rate;

    private int[] photo = {R.mipmap.china, R.mipmap.japan, R.mipmap.europe, R.mipmap.unitedstates, R.mipmap.southkorea};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_rate);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        ivRateImage = (ImageView) findViewById(R.id.iv_rate_image);
        tvZimu = (TextView) findViewById(R.id.tv_zimu);
        tvRateMoney = (TextView) findViewById(R.id.tv_rate_money);


        china = (EditText) findViewById(R.id.et_rmb);
        usd = (EditText) findViewById(R.id.et_dollers);
        eur = (EditText) findViewById(R.id.et_yingbang);
        random = (EditText) findViewById(R.id.et_random);
    }

    private void initData() {

        Intent intent = getIntent();
        tvZimu.setText(intent.getStringExtra("currency_code"));
        tvRateMoney.setText(intent.getStringExtra("currency_display"));
        id = intent.getIntExtra("id", 0);
        switch (id) {
            case 55:
                ivRateImage.setImageResource(photo[1]);
                rate = 0.06;
                break;
            case 57:
                ivRateImage.setImageResource(photo[3]);
                rate = 6.6;
                break;
            case 67:
                ivRateImage.setImageResource(photo[2]);
                rate = 8.8;
                break;
            case 47:
                ivRateImage.setImageResource(photo[4]);
                rate = 0.006;
                break;
            default:
                ivRateImage.setImageResource(photo[0]);
                rate = 1;
                break;
        }

    }

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);

        //人民币的点击事件
        china.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

//                random.setText(null);
//                usd.setText(null);
//                eur.setText(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!"".equals(china.getText().toString().trim())) {
                    double name = Double.valueOf(china.getText().toString().trim());
                    usd.setText("" + name / 6.6);
                    eur.setText("" + name / 8.8);
                    random.setText("" + name / rate);
                    random.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                    usd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                    eur.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if ("".equals(china.getText().toString().trim())) {
                    random.setText(null);
                    usd.setText(null);
                    eur.setText(null);
                }
            }
        });
        //美元的点击事件
        usd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                china.setText(null);
//                random.setText(null);
//                eur.setText(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //英镑的点击事件
        eur.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                china.setText(null);
//                usd.setText(null);
//                random.setText(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //一直改变的money的点击事件
        random.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                china.setText(null);
//                usd.setText(null);
//                eur.setText(null);

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                Intent intent = new Intent(ToolRateActivity.this, MainActivity.class);
                intent.putExtra("xia", 2);
                startActivity(intent);
                finish();
                break;

        }
    }
}
